package co.com.jtune.view.main;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

import org.jdesktop.application.ApplicationContext;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.TaskService;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

import co.com.jtune.JTuneApp;

public class JTuneFrame extends JFrame{
	
	public final static int MIN_WIDTH_FORM = 800;
	public final static int MIN_HEIGTH_FORM = 400;
	
	private ToolBarPanel toolBarPanel;
	private PlayListPanel playListPanel;
	private MusicListPanel musicListPanel;
	private BottomPanel bottomPanel;
	private MenuBarPanel menuBarPanel;
	private PlayingInfoPanel playingInfoPanel;
	private SearchPanel searchPanel;
	
	private SingleFrameApplication app;
	
	private ApplicationContext context;
	private TaskService taskService;
	private ResourceMap resource;

	private static final long serialVersionUID = 1L;
	
	public JTuneFrame ()
	{
		
		app = JTuneApp.getApplication();
		
		context = app.getContext();
		taskService = context.getTaskService();
		resource = context.getResourceMap(this.getClass());
		resource.injectComponents(this.getContentPane());
		
		buildComponets();
		addListener();
		createSimpleGui();
		
		this.setTitle(resource.getString("title"));
		if (resource.getImageIcon("icon") != null) {
			this.setIconImage(resource.getImageIcon("icon").getImage());
		}
	}
	
	private void addListener()
	{
		menuBarPanel.addPropertyChangeListener(menuBarPanel.LIBRARY_CHANGE, new PropertyChangeListener() {
			
			public void propertyChange(PropertyChangeEvent paramPropertyChangeEvent) {
				musicListPanel.refreshTable();
			}
		});
	}
	
	private void buildComponets()
	{
		//Crea un panel principal donde se insertan los otros paneles
		playListPanel = new PlayListPanel();
		musicListPanel = new MusicListPanel(resource);
		toolBarPanel = new ToolBarPanel(resource, musicListPanel);
		bottomPanel = new BottomPanel(resource);
		menuBarPanel = new MenuBarPanel(resource);
		playingInfoPanel = new PlayingInfoPanel(resource);
		searchPanel = new SearchPanel(resource);
	}
	
	private void createSimpleGui()
	{		
		setLayout(new BorderLayout());
		
		add(buildNorthPanel(), BorderLayout.NORTH);
		add(musicListPanel, BorderLayout.CENTER);
		add(playListPanel, BorderLayout.EAST);
		add(bottomPanel, BorderLayout.SOUTH);
		
		pack();
		setMinimumSize(new Dimension(MIN_WIDTH_FORM, MIN_HEIGTH_FORM));
		setVisible(true);
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	}
	
	private JPanel buildNorthPanel()
	{
		JPanel northPanel = new JPanel();
		FormLayout northPanelLayout = new FormLayout( "5px, 250px:grow, 5px, pref:grow, 5px, 200px:grow, 5px",	"pref, 5px, pref:grow, 5px");		
		northPanel.setLayout(northPanelLayout);
		CellConstraints cc = new CellConstraints();
		
		northPanel.add(menuBarPanel, cc.xywh(1, 1, 7, 1, CellConstraints.FILL, CellConstraints.FILL));
		northPanel.add(toolBarPanel, cc.xywh(2, 3, 1, 1, CellConstraints.FILL, CellConstraints.FILL));
		northPanel.add(playingInfoPanel, cc.xywh(4, 3, 1, 1, CellConstraints.FILL, CellConstraints.FILL));
		northPanel.add(searchPanel, cc.xywh(6, 3, 1, 1, CellConstraints.FILL, CellConstraints.FILL));
		
		return northPanel;
	}
	
	public TaskService getTaskService()
	{
		return taskService;
	}
}
